package alone.redis.queue.redisqueue.service;

import alone.redis.queue.redisqueue.util.RedisUtil;
import redis.clients.jedis.Jedis;

/**
 * redis订阅发布模式发布者
 */
public class MessagePublish extends Thread {

    private static final String channelName = "channel:publish";

    @Override
    public void run() {
        for (int i=0;i<5;i++){
            publishMessage(Thread.currentThread().getName() + " publish " + i);
        }
    }

    private void publishMessage(String message){
        Jedis jedis = RedisUtil.getJedis();
        Long publishNum = jedis.publish(channelName,message);
        System.out.println("publishMessage：" + message + "  ||  publishNum：" + publishNum);
    }


    public static void main(String[] args) {
        MessagePublish messagePublish = new MessagePublish();
        Thread thread1 = new Thread(messagePublish,"thread1");
        Thread thread2 = new Thread(messagePublish,"thread2");
        Thread thread3 = new Thread(messagePublish,"thread3");
        thread1.start();
        thread2.start();
        thread3.start();
    }

}
